//
//  Animations.swift
//  elarc01
//
//  Created by elarc on 16/4/15.
//  Copyright © 2016年 elarc. All rights reserved.
//

import Foundation
class Animations:NSObject {
    class func showAnimationOnclickHeart(animateView:UIView) {
        let bounceAnimation = CAKeyframeAnimation(keyPath: "transform.scale")
        bounceAnimation.values = [1.0 ,1.4, 0.9, 1.15, 0.95, 1.02, 1.0]
        bounceAnimation.duration = NSTimeInterval(0.6)
        bounceAnimation.calculationMode = kCAAnimationCubic
        animateView.layer.addAnimation(bounceAnimation, forKey: "bounceAnimation")
    }
    
    class func showAnimationOnclickPrice(animateView:UIView) {
        let bounceAnimation = CAKeyframeAnimation(keyPath: "transform.scale")
        bounceAnimation.values = [0.1,1.0,1.5]
        bounceAnimation.keyTimes = [0.0,0.5,0.8,1.0]
        bounceAnimation.calculationMode = kCAAnimationLinear
        animateView.layer.addAnimation(bounceAnimation, forKey:"SHOW")
    }
    
    class func showAnimationWithLeftToRight(animateView:UIView) {
        let bounceAnimation = CAKeyframeAnimation(keyPath: "transform.rotation")
        bounceAnimation.values = [-M_PI_4/90*5,M_PI_4/90*5,-M_PI_4/90*5]
        bounceAnimation.removedOnCompletion = false
        bounceAnimation.fillMode = kCAFillModeForwards
        bounceAnimation.duration = 0.2
        bounceAnimation.repeatCount = MAXFLOAT
//        bounceAnimation.calculationMode = kCAAnimationLinear
        animateView.layer.addAnimation(bounceAnimation, forKey:nil)

    }
}


//import Foundation
//
//class MJDIYHeader: MJRefreshHeader {
//    
//    var label:UILabel!
//    var logo:UIImageView!
//    
//    var gifView:UIView!
//    //    var loading:UIActivityIndicatorView!
//    
//    var lineLayer:CAShapeLayer!
//    var topPointLayer:CAShapeLayer!
//    var bottomPointLayer:CAShapeLayer!
//    var leftPointLayer:CAShapeLayer!
//    var rightPointLayer:CAShapeLayer!
//    
//    let refreshHeight:CGFloat = 35.0
//    let refreshPointRadius:CGFloat = 5.0
//    let refreshPulllen:CGFloat = 55.0
//    let refreshTranslatlen:CGFloat = 5.0
//    
//    let topColor = UIColor.blackColor()
//    let leftColor = UIColor.redColor()
//    let rightColor = UIColor.blueColor()
//    let bottomColor = UIColor.yellowColor()
//    let lineColor = UIColor.greenColor()
//    
//    var animating:Bool = false
//    var progress:CGFloat = 0 {
//        didSet {
//            if !self.animating {
//                self.setLineLayerStrokeWithProgress(progress)
//            }
//        }
//    }
//    
//    override func prepare()
//    {
//        super.prepare()
//        
//        self.mj_h = 50
//        
//        self.label =  UILabel()
//        self.label.textColor = UIColor.purpleColor()
//        self.label.font = UIFont.boldSystemFontOfSize(16)
//        self.label.textAlignment = NSTextAlignment.Center
//        self.addSubview(self.label)
//        
//        self.logo =  UIImageView(image:UIImage(named: "icon_placeholder"))
//        self.logo.contentMode = UIViewContentMode.ScaleAspectFit
//        self.addSubview(self.logo)
//        
//        gifView = UIImageView()
//        gifView.frame = CGRectMake(0, 0, refreshHeight, refreshHeight)
//        self.addSubview(gifView)
//        
//        buildLayers()
//        
//    }
//    
//    func buildLayers() -> Void {
//        let centerLine:CGFloat = refreshHeight / 2
//        let radius:CGFloat = refreshPointRadius
//        
//        let topPoint:CGPoint = CGPointMake(centerLine, radius)
//        self.topPointLayer = self.layerWithPoint(topPoint, color:topColor)
//        self.topPointLayer.hidden = false
//        self.topPointLayer.opacity = 0.0
//        gifView.layer.addSublayer(self.topPointLayer)
//        
//        let leftPoint:CGPoint = CGPointMake(radius, centerLine)
//        self.leftPointLayer = self.layerWithPoint(leftPoint, color:leftColor)
//        gifView.layer.addSublayer(self.leftPointLayer)
//        
//        let bottomPoint:CGPoint = CGPointMake(centerLine, refreshHeight - radius)
//        self.bottomPointLayer = self.layerWithPoint(bottomPoint, color:bottomColor)
//        gifView.layer.addSublayer(self.bottomPointLayer)
//        
//        let rightPoint:CGPoint = CGPointMake(refreshHeight - radius, centerLine)
//        self.rightPointLayer = self.layerWithPoint(rightPoint, color:rightColor)
//        gifView.layer.addSublayer(self.rightPointLayer)
//        
//        self.lineLayer = CAShapeLayer()
//        self.lineLayer.frame = gifView.bounds
//        self.lineLayer.lineWidth = radius * 2
//        self.lineLayer.lineCap = kCALineCapRound
//        self.lineLayer.lineJoin = kCALineJoinRound
//        self.lineLayer.fillColor = lineColor.CGColor
//        self.lineLayer.strokeColor = lineColor.CGColor
//        
//        let path = UIBezierPath()
//        path.moveToPoint(topPoint)
//        path.addLineToPoint(leftPoint)
//        path.moveToPoint(leftPoint)
//        path.addLineToPoint(bottomPoint)
//        path.moveToPoint(bottomPoint)
//        path.addLineToPoint(rightPoint)
//        path.moveToPoint(rightPoint)
//        path.addLineToPoint(topPoint)
//        self.lineLayer.path = path.CGPath
//        self.lineLayer.strokeStart = 0.0
//        self.lineLayer.strokeEnd = 0.0
//        gifView.layer.insertSublayer(self.lineLayer, above: self.topPointLayer)
//    }
//    
//    func layerWithPoint(center:CGPoint,color:UIColor) -> CAShapeLayer {
//        let layer = CAShapeLayer()
//        layer.frame = CGRectMake(center.x - refreshPointRadius, center.y - refreshPointRadius, refreshPointRadius * 2, refreshPointRadius * 2)
//        layer.fillColor = color.CGColor
//        layer.path = self.pointPath()
//        layer.hidden = true
//        return layer
//    }
//    
//    func pointPath() -> CGPathRef {
//        return UIBezierPath(arcCenter:CGPointMake(refreshPointRadius,refreshPointRadius), radius: refreshPointRadius, startAngle: 0, endAngle: CGFloat(M_PI * 2), clockwise: true).CGPath
//    }
//    
//    override func placeSubviews()
//    {
//        super.placeSubviews()
//        self.label.frame = self.bounds
//        
//        self.logo.backgroundColor = UIColor.clearColor()
//        self.logo.bounds = CGRect(x:0, y:0, width:self.bounds.size.width, height:50)
//        self.logo.center = CGPoint(x:self.mj_w * 0.5, y:-30)
//        gifView.center = CGPoint(x:self.mj_w * 0.5, y:30)
//        
//    }
//    
//    override var state: MJRefreshState{
//        didSet
//        {
//            switch (state) {
//            case MJRefreshStateIdle:
//                removeAni()
//                self.label.text = "赶紧下拉吖"
//                break
//            case MJRefreshStatePulling:
//                self.label.text = "赶紧放开我吧"
//                break
//            case MJRefreshStateRefreshing:
//                self.label.text = "加载数据中"
//                startAni()
//            case MJRefreshStateNoMoreData:
//                removeAni()
//                break
//            default:
//                break
//            }
//        }
//    }
//    
//    override var pullingPercent: CGFloat {
//        didSet
//        {
//            let  red =  1.0 - pullingPercent * 0.5
//            let green =  0.5 - 0.5 * pullingPercent
//            let blue =  0.5 * pullingPercent
//            self.label.textColor = UIColor(red:red, green:green, blue:blue, alpha:1.0)
//        }
//    }
//    
//    //监听scrollView的contentOffset改变
//    
//    override func scrollViewContentOffsetDidChange(change: [NSObject : AnyObject]!) {
//        super.scrollViewContentOffsetDidChange(change)
//        progress = -self.scrollView.contentOffset.y
//    }
//    
//    //监听scrollView的contentSize改变
//    override func scrollViewContentSizeDidChange(change: [NSObject : AnyObject]!) {
//        super.scrollViewContentSizeDidChange(change)
//    }
//    
//    //监听scrollView的拖拽状态改变
//    override func scrollViewPanStateDidChange(change: [NSObject : AnyObject]!) {
//        super.scrollViewPanStateDidChange(change)
//    }
//    
//    func setLineLayerStrokeWithProgress(progress:CGFloat) -> Void {
//        
//        var startProgress:CGFloat = 0
//        var endProgress:CGFloat = 0
//        
//        if progress < 0 {
//            self.topPointLayer.opacity = 0
//            self.adjustPointStateWithIndex(0)
//        }
//        else if progress >= 0 && progress < (refreshPulllen - 40) {
//            self.topPointLayer.opacity = Float(fabsf(Float(progress)) / 20)
//            self.adjustPointStateWithIndex(0)
//        }
//        else if progress >= (refreshPulllen - 40) && progress < refreshPulllen {
//            self.topPointLayer.opacity = 1.0
//            
//            let stage = (progress - refreshPulllen - 40) / 10
//            let subProgress:CGFloat = CGFloat(progress - (refreshPulllen - 40)) - CGFloat(stage * 10)
//            if subProgress >= 0 && subProgress <= 5 {
//                self.adjustPointStateWithIndex(Int(stage) * 2)
//                startProgress = stage / 4.0;
//                endProgress = stage / 4.0 + subProgress / 40.0 * 2
//            }
//            if subProgress > 5 && subProgress < 10 {
//                self.adjustPointStateWithIndex(Int(stage) * 2 + 1)
//                startProgress = stage / 4.0 + (subProgress - 5) / 40.0 * 2
//                if (startProgress < (stage + 1) / 4.0 - 0.1) {
//                    startProgress = (stage + 1) / 4.0 - 0.1;
//                }
//                endProgress = (stage + 1) / 4.0
//            }
//        }
//        else {
//            self.topPointLayer.opacity = 1.0
//            self.adjustPointStateWithIndex(NSIntegerMax)
//            startProgress = 1.0;
//            endProgress = 1.0;
//        }
//        self.lineLayer.strokeStart = startProgress
//        self.lineLayer.strokeEnd = endProgress
//    }
//    
//    func adjustPointStateWithIndex(index:Int) -> Void {
//        self.leftPointLayer.hidden = index > 1 ? false:true
//        self.bottomPointLayer.hidden = index > 3 ? false:true
//        self.rightPointLayer.hidden = index > 5 ? false:true
//        self.lineLayer.strokeColor = index > 5 ? rightColor.CGColor:index > 3 ? bottomColor.CGColor : index > 1 ? leftColor.CGColor : topColor.CGColor
//    }
//    
//    func startAni() -> Void {
//        self.animating = true
//        self.addTranslationAniToLayer(self.topPointLayer, xValue: 0, yValue: refreshTranslatlen)
//        self.addTranslationAniToLayer(self.leftPointLayer, xValue: refreshTranslatlen, yValue: 0)
//        self.addTranslationAniToLayer(self.bottomPointLayer, xValue: 0, yValue: -refreshTranslatlen)
//        self.addTranslationAniToLayer(self.rightPointLayer, xValue: -refreshTranslatlen, yValue: 0)
//        
//        self.addRotationAniToLayer(gifView.layer)
//    }
//    
//    func addTranslationAniToLayer(layer:CALayer,xValue:CGFloat,yValue:CGFloat) -> Void {
//        
//        let animation = CAKeyframeAnimation(keyPath: "transform")
//        
//        let rotation1:CATransform3D = CATransform3DMakeTranslation(0, 0, 0)
//        let rotation2:CATransform3D = CATransform3DMakeTranslation(xValue, yValue, 0)
//        animation.values = [NSValue(CATransform3D: rotation1),NSValue(CATransform3D: rotation2),NSValue(CATransform3D: rotation1), NSValue(CATransform3D: rotation2),NSValue(CATransform3D: rotation1)]
//        
//        animation.duration = 1.0
//        animation.repeatCount = HUGE
//        animation.removedOnCompletion = false
//        animation.fillMode = kCAFillModeForwards
//        animation.timingFunction = CAMediaTimingFunction.init(name: kCAMediaTimingFunctionLinear)
//        layer.addAnimation(animation, forKey: "translationKeyframeAni")
//    }
//    
//    func addRotationAniToLayer(layer:CALayer) -> Void {
//        
//        let rotationAni = CABasicAnimation(keyPath:"transform.rotation.z")
//        rotationAni.fromValue = 0
//        rotationAni.toValue = M_PI * 2
//        rotationAni.duration = 1.0;
//        rotationAni.timingFunction = CAMediaTimingFunction.init(name: kCAMediaTimingFunctionLinear)
//        rotationAni.repeatCount = HUGE;
//        rotationAni.fillMode = kCAFillModeForwards;
//        rotationAni.removedOnCompletion = false
//        layer.addAnimation(rotationAni, forKey: "rotationAni")
//    }
//    
//    func removeAni() -> Void {
//        UIView.animateWithDuration(0.5, animations: {
//            //            var inset = self.scrollView.contentInset
//            //                inset.top = 0
//            //                self.scrollView.contentInset = inset
//        }) { (finished) in
//            self.topPointLayer.removeAllAnimations()
//            self.leftPointLayer.removeAllAnimations()
//            self.bottomPointLayer.removeAllAnimations()
//            self.rightPointLayer.removeAllAnimations()
//            self.gifView.layer.removeAllAnimations()
//            self.adjustPointStateWithIndex(0)
//            self.animating = false
//        }
//    }
//}